
Imports std
Imports System.IO

Namespace language
    Namespace CJKShare
        Public Class easyIME
            Protected Friend trie As trie(Of Byte, String, _maxUInt8, _byte_to_uint32) = Nothing

            Public Sub initial(ByVal dict As String)
                Dim reader As StreamReader = Nothing
                Dim input As String = Nothing
                Dim parts As vector(Of String) = Nothing
                trie = New trie(Of Byte, String, _maxUInt8, _byte_to_uint32)()

                reader = New StreamReader(New FileStream(dict, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                While Not reader.EndOfStream
                    input = reader.ReadLine
                    strsepar(input, character.tab, parts)
                    If parts.size() = 2 Then
                        trie.insert(encoding.unicode.GetBytes(parts(0)), parts(1))
                    End If
                End While
                closestream(reader)
            End Sub

            Public Function any2pron(ByVal s As String) As String
                Dim rtn As String = emptyString
                Dim result As trie(Of Byte, String, _maxUInt8, _byte_to_uint32).iterator = Nothing

                If Not trie Is Nothing Then
                    While Not isemptystring(s)
                        result = trie.findfront(encoding.unicode.GetBytes(s))
                        If result <> trie.end() Then
                            rtn += (+result).value
                            s = strmid(s, (+result).length >> 1)
                        Else
                            rtn += s(0)
                            s = strmid(s, 1)
                        End If
                    End While
                End If

                Return rtn
            End Function

            Public Sub New()

            End Sub

            Public Sub New(ByVal dict As String)
                initial(dict)
            End Sub
        End Class
    End Namespace
End Namespace
